
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import javax.print.*;
import java.awt.print.*;

class MyFrame extends JFrame implements Serializable {

    protected DesignPanel panel;
    protected ArrayList<MyComponentContainer> componentList;
    protected Vector selectedComponentList;
    private int clickX,  clickY;
    private Shape shape;
    private int panelResize;
    protected int[] namesCount = new int[14];
    private int x1,  y1,  width,  height;
    protected MainPanel mainPanel;
    protected boolean flagDirty;
    protected boolean flagNewDesign;
    protected ArrayList<UndoHistoryInformation> undoHistory;
    protected int undoIndex;
    protected boolean formatMode = true;
    protected boolean gridMode = true;
    protected boolean formDesignMode = true;
    protected boolean toolBoxMode = true;
    MyComponentContainer component;
    String tableName;
    // Toolbox
    protected MyToolBox toolbox;
    // Toolbar
    protected MyToolBar toolbar = new MyToolBar(this);
    protected MyFormattingToolBar formatToolBar;
    JPanel panelToolBars;
    // Menu
    private JMenuBar menuBar = new JMenuBar();
    private JMenu fileMenu = new JMenu("File");
    protected JMenuItem saveItem = new JMenuItem("Save (CTRL+S)", new ImageIcon(getClass().getResource("/Images/Save.png")));
    private JMenuItem saveAsItem = new JMenuItem("      Save As...");
    private JMenuItem exportItem = new JMenuItem("      Export...");
    private JMenuItem printItem = new JMenuItem("Print (CTRL+P)", new ImageIcon(getClass().getResource("/Images/Print.png")));
    private JMenuItem printPreviewItem = new JMenuItem("Print Preview", new ImageIcon(getClass().getResource("/Images/PrintPreview.png")));
    private JMenuItem closeItem = new JMenuItem("      Close");
    private JMenu editMenu = new JMenu("Edit");
    protected JMenuItem undoItem = new JMenuItem("Undo (CTRL+Z)", new ImageIcon(getClass().getResource("/Images/Undo.png")));
    protected JMenuItem redoItem = new JMenuItem("Redo (CTRL+Y)", new ImageIcon(getClass().getResource("/Images/Redo.png")));
    protected JMenuItem deleteItem = new JMenuItem("Delete (DELETE)", new ImageIcon(getClass().getResource("/Images/Delete.png")));
    private JMenuItem selectAllItem = new JMenuItem("      Select All (CTRL+A)");
    protected JMenuItem copyItem = new JMenuItem("Copy (CTRL+C)", new ImageIcon(getClass().getResource("/Images/Copy.png")));
    protected JMenuItem cutItem = new JMenuItem("Cut (CTRL+X)", new ImageIcon(getClass().getResource("/Images/Cut.png")));
    protected JMenuItem pasteItem = new JMenuItem("Paste (CTRL+V)", new ImageIcon(getClass().getResource("/Images/Paste.png")));
    protected JMenuItem duplicateItem = new JMenuItem("      Duplicate");
    private JMenu viewMenu = new JMenu("View");
    private JCheckBoxMenuItem designViewItem = new JCheckBoxMenuItem("Design View");
    private JCheckBoxMenuItem formViewItem = new JCheckBoxMenuItem("Form View");
    private JMenuItem propertiesItem = new JMenuItem("Properties");
    private JCheckBoxMenuItem gridItem = new JCheckBoxMenuItem("Grid");
    protected JCheckBoxMenuItem toolBoxItem = new JCheckBoxMenuItem("ToolBox");
    protected JCheckBoxMenuItem toolBox2Item = new JCheckBoxMenuItem("ToolBox");
    private JCheckBoxMenuItem formDesignItem = new JCheckBoxMenuItem("Form Design");
    private JCheckBoxMenuItem formatItem = new JCheckBoxMenuItem("Formatting");
    private JMenu toolBarMenu = new JMenu("   ToolBars");
    private JMenu helpMenu = new JMenu("Help");
    private JMenuItem aboutItem = new JMenuItem("About");
    //#########################################################################

    public int getX1() {
        return x1;
    }

    public int getY1() {
        return y1;
    }

    public int getDragWidth() {
        return width;
    }

    public int getDragHeight() {
        return height;
    }
    //#########################################################################

    /**
     * Get the last sequence number of added component
     */
    public int getCount(int indx) {
        return namesCount[indx];
    }

    public JPanel getPanel() {
        return panel;
    }

    /**
     * Increment the sequence number of a component
     */
    public void incCount(int indx) {
        namesCount[indx]++;
    }
    //#########################################################################

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String table) {
        tableName = table;
    }
    //#########################################################################

    MyFrame(Dimension screenDimension) {
        addWindowListener(new FrameHandler());
        //kit=Toolkit.getDefaultToolkit();
        selectedComponentList = new Vector();
        componentList = new ArrayList<MyComponentContainer>();
        undoHistory = new ArrayList<UndoHistoryInformation>();

        //add Menu Handlers
        MenuHandler menuHandler = new MenuHandler();
        closeItem.addActionListener(menuHandler);
        saveItem.addActionListener(menuHandler);
        saveAsItem.addActionListener(menuHandler);
        undoItem.addActionListener(menuHandler);
        redoItem.addActionListener(menuHandler);
        copyItem.addActionListener(menuHandler);
        cutItem.addActionListener(menuHandler);
        pasteItem.addActionListener(menuHandler);
        deleteItem.addActionListener(menuHandler);
        selectAllItem.addActionListener(menuHandler);
        duplicateItem.addActionListener(menuHandler);
        formViewItem.addActionListener(menuHandler);
        propertiesItem.addActionListener(menuHandler);
        gridItem.addActionListener(menuHandler);
        toolBox2Item.addActionListener(menuHandler);
        toolBoxItem.addActionListener(menuHandler);
        formDesignItem.addActionListener(menuHandler);
        formatItem.addActionListener(menuHandler);
        aboutItem.addActionListener(menuHandler);

        // Menu set
        fileMenu.add(saveItem);
        saveItem.setEnabled(false);
        fileMenu.add(saveAsItem);
        fileMenu.add(exportItem);
        fileMenu.addSeparator();
        fileMenu.add(printItem);
        fileMenu.add(printPreviewItem);
        fileMenu.addSeparator();
        fileMenu.add(closeItem);

        editMenu.add(undoItem);
        editMenu.add(redoItem);
        undoItem.setEnabled(false);
        redoItem.setEnabled(false);
        editMenu.addSeparator();
        editMenu.add(cutItem);
        cutItem.setEnabled(false);
        editMenu.add(copyItem);
        copyItem.setEnabled(false);
        editMenu.add(pasteItem);
        if (MainWindow.copiedComponents.isEmpty()) {
            pasteItem.setEnabled(false);
        }
        editMenu.addSeparator();
        editMenu.add(duplicateItem);
        duplicateItem.setEnabled(false);
        editMenu.add(selectAllItem);
        editMenu.addSeparator();
        editMenu.add(deleteItem);
        deleteItem.setEnabled(false);

        ButtonGroup buttonGroupDesign = new ButtonGroup();
        buttonGroupDesign.add(designViewItem);
        buttonGroupDesign.add(formViewItem);
        designViewItem.setSelected(true);
        viewMenu.add(designViewItem);
        designViewItem.setSelected(true);
        viewMenu.add(formViewItem);
        viewMenu.addSeparator();
        viewMenu.add(propertiesItem);
        viewMenu.addSeparator();
        viewMenu.add(gridItem);
        gridItem.setSelected(true);
        viewMenu.add(toolBoxItem);
        toolBoxItem.setSelected(true);
        toolBox2Item.setSelected(true);
        viewMenu.addSeparator();
        toolBarMenu.add(formDesignItem);
        formDesignItem.setSelected(true);
        formatItem.setSelected(true);
        toolBarMenu.add(formatItem);
        toolBarMenu.add(toolBox2Item);
        viewMenu.add(toolBarMenu);


        helpMenu.add(aboutItem);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(viewMenu);
        menuBar.add(helpMenu);
        setJMenuBar(menuBar);

        panel = new DesignPanel();
        panel.setLayout(null);
        panel.setBackground(Color.WHITE);
        panel.addMouseListener(new PanelHandler());
        panel.addKeyListener(new KeyPressHandler());
        panel.addMouseMotionListener(new MotionHandler());
        panel.setSize(640, 480);
        panel.setLocation(0, 0);
        mainPanel = new MainPanel(panel);
        mainPanel.drawPanel.add(panel);
        add(mainPanel);

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        Dimension d = screenDimension;
        setSize((int) d.getWidth(), (int) d.getHeight());

        refreshScrolls();

        toolbox = new MyToolBox(panel);
        formatToolBar = new MyFormattingToolBar(this);

        add(toolbox, BorderLayout.WEST);
        panelToolBars = new JPanel();
        panelToolBars.setLayout(new GridLayout(2, 1));
        panelToolBars.add(toolbar);
        panelToolBars.add(formatToolBar);

        add(panelToolBars, BorderLayout.NORTH);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        UndoHistoryInformation undoHistoryInfo = new UndoHistoryInformation(componentList, panel.getSize(), panel.getBackground());
        undoHistory.add(undoHistoryInfo);
        undoIndex = 0;
    }
    //#########################################################################

    /**
     * Design panel with grid mode and adjustable grid spacing
     */
    public class DesignPanel extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (gridMode) {
                Graphics2D g2d = (Graphics2D) g;
                int width = getWidth();
                int height = getHeight();
                if (shape != null) {
                    g2d.draw(shape);
                }
                for (int i = 0; i < width; i += MainWindow.gridSpacing) {
                    g2d.drawLine(i, 0, i, height);
                }
                for (int i = 0; i < height; i += MainWindow.gridSpacing) {
                    g2d.drawLine(0, i, width, i);
                }
            }
        }
    }
    //#########################################################################

    private class KeyPressHandler extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (e.isControlDown()) {
                if (key == KeyEvent.VK_C) {
                    toolbar.copy();
                } else if (key == KeyEvent.VK_X) {
                    toolbar.cut();
                } else if (key == KeyEvent.VK_V && MainWindow.copiedComponents.isEmpty() == false) {
                    toolbar.paste();
                } else if (key == KeyEvent.VK_Z && toolbar.toolUndo.isEnabled()) {
                    toolbar.undo();
                } else if (key == KeyEvent.VK_Y && toolbar.toolRedo.isEnabled()) {
                    toolbar.redo();
                } else if (key == KeyEvent.VK_A) {
                    selectAllComponents();
                } else if (key == KeyEvent.VK_S) {
                    save();
                } else if (key == KeyEvent.VK_P) {
                    boolean flagPrint = false;
                    PrinterJob printerJob = PrinterJob.getPrinterJob();
                    flagPrint = printerJob.printDialog();
                    if (flagPrint) {
                        MyFileChooser myFileChooser = new MyFileChooser();
                        myFileChooser.printFileChooserSelection = myFileChooser.jFileChooser1.showSaveDialog(null);
                    }
                }
            } else {
                switch (key) {
                    case (KeyEvent.VK_UP):
                        boolean flagDrag = false;
                        for (MyComponentContainer c : componentList) {
                            if (c.isChosen()) {
                                int y;
                                y = (int) c.getComponent().getY();
                                int newY = y - 5;
                                if (newY <= 5) {
                                    flagDrag = true;
                                    flagDirty = true;
                                    toolbar.toolSave.setEnabled(true);
                                    saveItem.setEnabled(true);
                                }
                            }
                        }
                        if (flagDrag == false) {
                            for (MyComponentContainer c : componentList) {
                                if (c.isChosen()) {
                                    c.getComponent().setLocation(c.getComponent().getX(), c.getComponent().getY() - 5);
                                    c.showBorder();
                                    flagDirty = true;
                                    toolbar.toolSave.setEnabled(true);
                                    saveItem.setEnabled(true);
                                }
                            }
                        }
                        handleUndo();
                        break;
                    case (KeyEvent.VK_DOWN):
                        for (MyComponentContainer c : componentList) {
                            if (c.isChosen()) {
                                c.getComponent().setLocation(c.getComponent().getX(), c.getComponent().getY() + 5);
                                c.showBorder();
                                c.resizePanel();
                                flagDirty = true;
                                toolbar.toolSave.setEnabled(true);
                                saveItem.setEnabled(true);
                            }
                        }
                        handleUndo();
                        break;
                    case (KeyEvent.VK_RIGHT):
                        for (MyComponentContainer c : componentList) {
                            if (c.isChosen()) {
                                c.getComponent().setLocation(c.getComponent().getX() + 5, c.getComponent().getY());
                                c.showBorder();
                                c.resizePanel();
                                flagDirty = true;
                                toolbar.toolSave.setEnabled(true);
                                saveItem.setEnabled(true);
                            }
                        }
                        handleUndo();
                        break;
                    case (KeyEvent.VK_LEFT):
                        flagDrag = false;
                        for (MyComponentContainer c : componentList) {
                            if (c.isChosen()) {
                                int x;
                                x = (int) c.getComponent().getX();
                                int newX = x - 5;
                                if (newX <= 5) {
                                    flagDrag = true;
                                    flagDirty = true;
                                    toolbar.toolSave.setEnabled(true);
                                    saveItem.setEnabled(true);
                                }
                            }
                        }
                        if (flagDrag == false) {
                            for (MyComponentContainer c : componentList) {
                                if (c.isChosen()) {
                                    c.getComponent().setLocation(c.getComponent().getX() - 5, c.getComponent().getY());
                                    c.showBorder();
                                    flagDirty = true;
                                    toolbar.toolSave.setEnabled(true);
                                    saveItem.setEnabled(true);
                                }
                            }
                        }
                        handleUndo();
                        break;
                    case (KeyEvent.VK_DELETE):
                        deleteComponent();
                        break;

                    case (KeyEvent.VK_ESCAPE):
                        toolbox.setPressed(false);
                        panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                        panel.requestFocusInWindow();
                        removeComponentSelection();
                        break;

                    case (KeyEvent.VK_TAB):
                        boolean done = false;
                        for (MyComponentContainer c : componentList) {
                            if (done) {
                                if (c.isChosen() == false) {
                                    c.showBorder();
                                    c.setChosen(true);
                                }
                                break;
                            }
                            if (c.isChosen()) {
                                c.hideBorder();
                                c.setChosen(false);
                                done = true;
                            }
                        }
                        panel.repaint();
                }
            }
        }
    }
    //#########################################################################

    private class PanelHandler extends MouseAdapter {

        public void mouseClicked(MouseEvent a) {
            panel.requestFocusInWindow();
            removeComponentSelection();
            if (a.getButton() == MouseEvent.BUTTON3) {
                JPopupMenu panelPopupMenu = new JPopupMenu();
                JMenuItem propertiesPopItem = new JMenuItem("      Properties");
                JMenuItem selectAllPopItem = new JMenuItem("      Select All (CTRL+A)");
                JMenuItem pastePopItem = new JMenuItem("Paste (CTRL+V)", new ImageIcon(getClass().getResource("/Images/Paste.png")));
                JMenuItem backPopItem = new JMenuItem("Fill/Back Color", new ImageIcon(getClass().getResource("/Images/BackColor.png")));
                JCheckBoxMenuItem gridPopItem = new JCheckBoxMenuItem("   Grid");
                JCheckBoxMenuItem toolBoxPopItem = new JCheckBoxMenuItem("   ToolBox");

                PanelRightClickMenuHandler panelRightClickMenuHandler = new PanelRightClickMenuHandler();
                selectAllPopItem.addActionListener(panelRightClickMenuHandler);
                pastePopItem.addActionListener(panelRightClickMenuHandler);
                backPopItem.addActionListener(panelRightClickMenuHandler);
                gridPopItem.addActionListener(panelRightClickMenuHandler);
                toolBoxPopItem.addActionListener(panelRightClickMenuHandler);
                propertiesPopItem.addActionListener(panelRightClickMenuHandler);

                panelPopupMenu.add(selectAllPopItem);
                panelPopupMenu.add(pastePopItem);
                panelPopupMenu.add(backPopItem);
                panelPopupMenu.addSeparator();
                panelPopupMenu.add(gridPopItem);
                panelPopupMenu.add(toolBoxPopItem);
                panelPopupMenu.addSeparator();
                panelPopupMenu.add(propertiesPopItem);

                if (MainWindow.copiedComponents.isEmpty()) {
                    pastePopItem.setEnabled(false);
                }

                toolBoxPopItem.setSelected(toolBoxMode);
                gridPopItem.setSelected(gridMode);

                panelPopupMenu.show(panel, a.getX(), a.getY());
            }
        }

        public void mousePressed(MouseEvent e) {
            clickX = e.getX();
            clickY = e.getY();
            if (e.getX() == (panel.getWidth() - 1) & e.getY() == (panel.getHeight() - 1)) {
                panelResize = 1;
            } else if (e.getX() == (panel.getWidth() - 1)) {
                panelResize = 2;
            } else if (e.getY() == (panel.getHeight() - 1)) {
                panelResize = 3;
            }
        }

        public void mouseReleased(MouseEvent e) {
            // Handling panel resize
            if (panelResize != 0) {
                int maxX = getMaxX();
                int maxY = getMaxY();
                if (panel.getWidth() < maxX & panel.getHeight() < maxY) {
                    panel.setSize(maxX + 10, maxY + 10);
                } else if (panel.getWidth() < maxX) {
                    panel.setSize(maxX + 10, panel.getHeight());
                } else if (panel.getHeight() < maxY) {
                    panel.setSize(panel.getWidth(), maxY + 10);
                }
                handleUndo();
            }

            // Handling toolbox selection
            if (toolbox.isPressed()) {
                toolbox.setPressed(false);
                int releasedX = e.getX();
                int releasedY = e.getY();
                component = null;
                if (releasedX == clickX && releasedY == clickY) {
                    x1 = clickX;
                    y1 = clickY;
                    width = height = 30;
                } else {
                    Rectangle bounds = shape.getBounds();
                    x1 = (int) bounds.getX();
                    y1 = (int) bounds.getY();
                    width = (int) bounds.getWidth();
                    height = (int) bounds.getHeight();

                }
                panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                switch (toolbox.getSelected()) {
                    case (MyToolBox.BUTTON):
                        new MyButtonWizardFrame(MyFrame.this);
                        break;
                    case (MyToolBox.COMBOBOX):
                        ComboBoxWizard1.comboBoxWizard1 = new ComboBoxWizard1(MyFrame.this);
                        ComboBoxWizard1.comboBoxWizard1.setVisible(true);
                        break;
                    case (MyToolBox.PICTURE):
                        MyPictureChooser pictureChooser = new MyPictureChooser(MyFrame.this);
                        File file = pictureChooser.getSelectedFile();
                        if (file != null) {
                            ImageIcon icon = new ImageIcon(file.getAbsolutePath());
                            namesCount[MyToolBox.PICTURE]++;
                            component = new MyPictureContainer("Picture" + namesCount[MyToolBox.PICTURE], MyFrame.this, icon);
                            component.getComponent().setName("Picture" + namesCount[MyToolBox.PICTURE]);
                        }
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    case (MyToolBox.CHECKBOX):
                        component = new MyCheckBoxContainer(MyFrame.this);
                        namesCount[MyToolBox.CHECKBOX]++;
                        component.getComponent().setName("Checkbox" + namesCount[MyToolBox.CHECKBOX]);
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    case (MyToolBox.LIST):
                        namesCount[MyToolBox.LIST]++;
                        component = new MyListContainer("Listbox" + namesCount[MyToolBox.LIST], MyFrame.this);
                        component.getComponent().setName("Listbox" + namesCount[MyToolBox.LIST]);
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    case (MyToolBox.TEXTFIELD):
                        component = new MyTextFieldContainer("Unbound", MyFrame.this);
                        namesCount[MyToolBox.TEXTFIELD]++;
                        component.getComponent().setName("Textbox" + namesCount[MyToolBox.TEXTFIELD]);
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    case (MyToolBox.TOGGLEBUTTON):
                        namesCount[MyToolBox.TOGGLEBUTTON]++;
                        component = new MyToggleButtonContainer("ToggleButton" + namesCount[MyToolBox.TOGGLEBUTTON], MyFrame.this);
                        component.getComponent().setName("ToggleButton" + namesCount[MyToolBox.TOGGLEBUTTON]);
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    case (MyToolBox.LABEL):
                        namesCount[MyToolBox.LABEL]++;
                        component = new MyLabelContainer("Label" + namesCount[MyToolBox.LABEL], MyFrame.this);
                        component.getComponent().setName("Label" + namesCount[MyToolBox.LABEL]);
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    case (MyToolBox.RADIOBUTTON):
                        component = new MyRadioButtonContainer(MyFrame.this);
                        namesCount[MyToolBox.RADIOBUTTON]++;
                        component.getComponent().setName("RadioButton" + namesCount[MyToolBox.RADIOBUTTON]);
                        // Set component size & location
                        if (component != null) {
                            addComponent(component);
                            component = null;
                        }
                        break;
                    default:
                        return;
                }
            } // Handling selection rectangle
            else if (shape != null) {
                // Free selected vector
                selectedComponentList.removeAllElements();
                int selectedComponentIndex = 0;
                for (MyComponentContainer c : componentList) {
                    if (shape.intersects(c.getComponent().getBounds())) {
                        selectedComponentList.add(c.getName());
                        if (c.isChosen() == false) {
                            c.showBorder();
                            c.setChosen(true);
                            selectedComponentIndex = componentList.indexOf(c);
                        }
                    } else {
                        if (c.isChosen()) {
                            c.hideBorder();
                            c.setChosen(false);
                        }
                    }
                }
                if (selectedComponentList.size() == 1) {
                    formatToolBar.setSelectedComponent((String) selectedComponentList.get(0));
                    formatToolBar.enableButtons(componentList.get(selectedComponentIndex));
                } else {
                    formatToolBar.setSelectedComponent(null);
                    formatToolBar.enableButtons(null);
                }
                if (selectedComponentList.size() > 0) {
                    duplicateItem.setEnabled(true);
                    deleteItem.setEnabled(true);
                    copyItem.setEnabled(true);
                    cutItem.setEnabled(true);
                    toolbar.toolCopy.setEnabled(true);
                    toolbar.toolCut.setEnabled(true);
                }
            }
            shape = null;
            panel.repaint();
            panel.requestFocusInWindow();
            panelResize = 0;
        }
    }
    //#########################################################################

    class MotionHandler extends MouseMotionAdapter {

        public void mouseDragged(MouseEvent e) {
            switch (panelResize) {
                case 1:
                    JPanel panel2 = (JPanel) e.getSource();
                    panel.setSize((int) (panel2.getParent().getMousePosition().getX()), (int) (panel2.getParent().getMousePosition().getY()));
                    autoscrollPanel();
                    validate();
                    repaint();
                    flagDirty = true;
                    toolbar.toolSave.setEnabled(true);
                    saveItem.setEnabled(true);
                    break;

                case 2:
                    panel2 = (JPanel) e.getSource();
                    if (panel2.getParent().getMousePosition().getX() >= 0) {
                        panel.setSize((int) (panel2.getParent().getMousePosition().getX()), panel.getHeight());
                    } else {
                        panel.setSize(0, panel.getHeight());
                    }
                    autoscrollPanel();
                    validate();
                    repaint();
                    flagDirty = true;
                    toolbar.toolSave.setEnabled(true);
                    saveItem.setEnabled(true);
                    break;

                case 3:
                    panel2 = (JPanel) e.getSource();
                    if (panel2.getParent().getMousePosition().getY() >= 1) {
                        panel.setSize(panel.getWidth(), (int) (panel2.getParent().getMousePosition().getY()));
                    } else {
                        panel.setSize(panel.getWidth(), 1);
                    }
                    autoscrollPanel();
                    validate();
                    repaint();
                    flagDirty = true;
                    toolbar.toolSave.setEnabled(true);
                    saveItem.setEnabled(true);
                    break;

                default:
                    float x1,
                     y1,
                     x2,
                     y2;
                    if (clickX > e.getX() && clickY > e.getY()) {
                        x1 = e.getX();
                        y1 = e.getY();
                        x2 = clickX;
                        y2 = clickY;
                    } else if (clickX > e.getX() && clickY < e.getY()) {
                        x1 = e.getX();
                        y1 = clickY;
                        x2 = clickX;
                        y2 = e.getY();
                    } else if (clickX < e.getX() && clickY > e.getY()) {
                        x1 = clickX;
                        y1 = e.getY();
                        x2 = e.getX();
                        y2 = clickY;
                    } else {
                        x1 = clickX;
                        y1 = clickY;
                        x2 = e.getX();
                        y2 = e.getY();
                    }
                    shape = new Rectangle2D.Float(x1, y1, x2 - x1, y2 - y1);
                    panel.repaint();
                    break;
            }
        }

        public void mouseMoved(MouseEvent m) {
            if (m.getX() == (panel.getWidth() - 1) & m.getY() == (panel.getHeight() - 1)) {
                panel.setCursor(new Cursor(Cursor.MOVE_CURSOR));
            } else if (m.getX() == (panel.getWidth() - 1)) {
                panel.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
            } else if (m.getY() == (panel.getHeight() - 1)) {
                panel.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
            } else if (toolbox.isPressed()) {
                panel.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            } else {
                panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        }
    }
    //#############################################################################

    public int getMaxX() {
        int maxX = 0;
        int x;
        for (MyComponentContainer c : componentList) {
            x = c.getComponent().getX() + c.getComponent().getWidth();
            if (x > maxX) {
                maxX = x;
            }
        }
        return maxX;
    }
    //#############################################################################

    public int getMaxY() {
        int maxY = 0;
        int y;
        for (MyComponentContainer c : componentList) {
            y = c.getComponent().getY() + c.getComponent().getHeight();
            if (y > maxY) {
                maxY = y;
            }
        }
        return maxY;
    }
    //##############################################################################

    public void autoscrollPanel() {
        if (panel.getVisibleRect().getWidth() > this.getWidth() - 150 & panel.getVisibleRect().getHeight() > this.getHeight() - 200) {
            panel.setLocation(panel.getX() - 1, panel.getY() - 1);
            panel.setSize(panel.getWidth() + 1, panel.getHeight() + 1);
            mainPanel.scrollHorizontal.setMaximum(mainPanel.scrollHorizontal.getMaximum() + 1);
            mainPanel.scrollHorizontal.setValue(mainPanel.scrollHorizontal.getMaximum());
            mainPanel.scrollHorizontalValue = mainPanel.scrollHorizontal.getValue();
            mainPanel.scrollVertical.setMaximum(mainPanel.scrollVertical.getMaximum() + 1);
            mainPanel.scrollVertical.setValue(mainPanel.scrollVertical.getMaximum());
            mainPanel.scrollVerticalValue = mainPanel.scrollVertical.getValue();
            mainPanel.scrollHorizontal.setVisible(true);
            mainPanel.scrollVertical.setVisible(true);
            mainPanel.drawPanel.validate();
            mainPanel.drawPanel.repaint();
            mainPanel.validate();
            mainPanel.repaint();
        } else if (panel.getVisibleRect().getWidth() > getWidth() - 100) {
            panel.setLocation(panel.getX() - 1, panel.getY());
            panel.setSize(panel.getWidth() + 1, panel.getHeight());
            mainPanel.scrollHorizontal.setMaximum(mainPanel.scrollHorizontal.getMaximum() + 1);
            //mainPanel.scrollHorizontal.setMaximum((panel.getWidth()-(int)panel.getVisibleRect().getWidth())+(int)mainPanel.drawPanel.getVisibleRect().getWidth()-(int)panel.getVisibleRect().getWidth());
            mainPanel.scrollHorizontal.setValue(mainPanel.scrollHorizontal.getMaximum());
            mainPanel.scrollHorizontalValue = mainPanel.scrollHorizontal.getValue();
            mainPanel.scrollHorizontal.setVisible(true);
            mainPanel.drawPanel.validate();
            mainPanel.drawPanel.repaint();
            mainPanel.validate();
            mainPanel.repaint();
        } else if (panel.getVisibleRect().getHeight() > this.getHeight() - 200) {
            panel.setLocation(panel.getX(), panel.getY() - 1);
            panel.setSize(panel.getWidth(), panel.getHeight() + 1);
            mainPanel.scrollVertical.setMaximum(mainPanel.scrollVertical.getMaximum() + 1);
            mainPanel.scrollVertical.setValue(mainPanel.scrollVertical.getMaximum());
            mainPanel.scrollVerticalValue = mainPanel.scrollVertical.getValue();
            mainPanel.scrollVertical.setVisible(true);
            mainPanel.drawPanel.validate();
            mainPanel.drawPanel.repaint();
            mainPanel.validate();
            mainPanel.repaint();
        }
    }
    //##########################################################################

    public void refreshScrolls() {
        if (panel.getWidth() > getWidth()) {
            mainPanel.scrollHorizontal.setVisible(true);
            mainPanel.scrollHorizontal.setMaximum(panel.getWidth() - getWidth() + 50);
        }
        if (panel.getHeight() > getHeight()) {
            mainPanel.scrollVertical.setVisible(true);
            mainPanel.scrollVertical.setMaximum(panel.getHeight() - getHeight() + 200);
        }
    }
    //###############################################################################

    public void refreshComponents(Dimension dimension, Color panelBackgroundColor, ArrayList<MyComponentContainer> fileComponentList, String tablename) {
        setTableName(tablename);
        panel.setSize(dimension);
        panel.setBackground(panelBackgroundColor);
        for (MyComponentContainer c : fileComponentList) {
            if (c instanceof MyLabelContainer) {
                c.getComponent().addMouseListener(((MyLabelContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyLabelContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyTextFieldContainer) {
                c.getComponent().addMouseListener(((MyTextFieldContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyTextFieldContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyCheckBoxContainer) {
                c.getComponent().addMouseListener(((MyCheckBoxContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyCheckBoxContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyRadioButtonContainer) {
                c.getComponent().addMouseListener(((MyRadioButtonContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyRadioButtonContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyButtonContainer) {
                c.getComponent().addMouseListener(((MyButtonContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyButtonContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyToggleButtonContainer) {
                c.getComponent().addMouseListener(((MyToggleButtonContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyToggleButtonContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyPictureContainer) {
                c.getComponent().addMouseListener(((MyPictureContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyPictureContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyListContainer) {
                c.getComponent().addMouseListener(((MyListContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyListContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            } else if (c instanceof MyComboBoxContainer) {
                c.getComponent().addMouseListener(((MyComboBoxContainer) c).new MouseHandler());
                c.getComponent().addMouseMotionListener(((MyComboBoxContainer) c).new MouseDragHandler());
                MyComponentContainer.ResizeHandler resizeHandler = c.new ResizeHandler();
                MyComponentContainer.BorderHandler borderHandler = c.new BorderHandler();
                c.dragAnchor.addMouseListener(borderHandler);
                c.dragAnchor.addMouseMotionListener(c.new DragHandler());
                for (int i = 0; i < 7; i++) {
                    c.resizeAnchor[i].addMouseListener(borderHandler);
                    c.resizeAnchor[i].addMouseMotionListener(resizeHandler);
                }
            }
            c.setChosen(false);
            c.setFrame(MyFrame.this);
            panel.add(c.getComponent());
            componentList.add(c);
        }
        undoHistory.clear();
        ArrayList<MyComponentContainer> myComponentList = new ArrayList<MyComponentContainer>();
        for (MyComponentContainer c : componentList) {
            myComponentList.add(ComponentClone.clone(c));
        }

        UndoHistoryInformation undoHistoryInfo = new UndoHistoryInformation(myComponentList, panel.getSize(), panel.getBackground());
        undoHistory.add(undoHistoryInfo);
        refreshScrolls();
    }
    //###########################################################################

    class FrameHandler extends WindowAdapter {

        public void windowClosing(WindowEvent e) {
            windowClose();
        }

        public void windowActivated(WindowEvent e) {
            toolbar.refreshPasteButton();
        }
    }
    //###########################################################################

    public void windowClose() {
        if (flagDirty) {
            int i = JOptionPane.showConfirmDialog(null, "Do You Want To Save Your Changes?", "Closing", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {
                if (flagNewDesign) {
                    String formName = JOptionPane.showInputDialog("Enter Form's Name:", getTitle());
                    if (formName != null) {
                        boolean flagNameExists = false;
                        for (String str : MainWindow.myForms) {
                            if (formName.equals(str)) {
                                flagNameExists = true;
                            }
                        }
                        if (flagNameExists) {
                            JOptionPane.showMessageDialog(MyFrame.this, "A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
                            setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                        } else if (formName.equals("")) {
                            JOptionPane.showMessageDialog(MyFrame.this, "Form Name Cannot Be Blank!");
                            setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                        } else {
                            Save.save(formName, panel, componentList, tableName);
                            MainWindow.refreshFormList(formName);
                            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                            MyFrame.this.dispose();
                        }
                    }
                } else {
                    Save.save(getTitle(), panel, componentList, tableName);
                    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                }
            } else if (i == JOptionPane.NO_OPTION) {
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                MyFrame.this.dispose();
            } else {
                setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
        }
    }
    //###########################################################################

    public void removeComponentSelection() {
        selectedComponentList.removeAllElements();
        for (MyComponentContainer c : componentList) {
            if (c.isChosen()) {
                c.hideBorder();
                c.setChosen(false);
            }
        }
        formatToolBar.setSelectedComponent("Panel");
        formatToolBar.disableButtons();
        duplicateItem.setEnabled(false);
        deleteItem.setEnabled(false);
        copyItem.setEnabled(false);
        cutItem.setEnabled(false);
        toolbar.toolCopy.setEnabled(false);
        toolbar.toolCut.setEnabled(false);
    }
    //###########################################################################

    public void initializeNamesCount() {
        for (MyComponentContainer component : componentList) {
            String name = component.getName();
            try {
                if (name.substring(0, 4).equals("List")) {
                    int number = Integer.parseInt(name.substring(4));
                    if (number > namesCount[MyToolBox.LIST]) {
                        namesCount[MyToolBox.LIST] = number;
                    }
                } else if (name.substring(0, 5).equals("Label")) {
                    int number = Integer.parseInt(name.substring(5));
                    if (number > namesCount[MyToolBox.LABEL]) {
                        namesCount[MyToolBox.LABEL] = number;
                    }
                } else if (name.substring(0, 6).equals("Button")) {
                    int number = Integer.parseInt(name.substring(6));
                    if (number > namesCount[MyToolBox.BUTTON]) {
                        namesCount[MyToolBox.BUTTON] = number;
                    }
                } else if (name.substring(0, 7).equals("Textbox")) {
                    int number = Integer.parseInt(name.substring(7));
                    if (number > namesCount[MyToolBox.TEXTFIELD]) {
                        namesCount[MyToolBox.TEXTFIELD] = number;
                    }
                } else if (name.substring(0, 7).equals("Picture")) {
                    int number = Integer.parseInt(name.substring(7));
                    if (number > namesCount[MyToolBox.PICTURE]) {
                        namesCount[MyToolBox.PICTURE] = number;
                    }
                } else if (name.substring(0, 8).equals("Checkbox")) {
                    int number = Integer.parseInt(name.substring(8));
                    if (number > namesCount[MyToolBox.CHECKBOX]) {
                        namesCount[MyToolBox.CHECKBOX] = number;
                    }
                } else if (name.substring(0, 8).equals("Combobox")) {
                    int number = Integer.parseInt(name.substring(8));
                    if (number > namesCount[MyToolBox.COMBOBOX]) {
                        namesCount[MyToolBox.COMBOBOX] = number;
                    }
                } else if (name.substring(0, 11).equals("RadioButton")) {
                    int number = Integer.parseInt(name.substring(11));
                    if (number > namesCount[MyToolBox.RADIOBUTTON]) {
                        namesCount[MyToolBox.RADIOBUTTON] = number;
                    }
                } else if (name.substring(0, 12).equals("ToggleButton")) {
                    int number = Integer.parseInt(name.substring(12));
                    if (number > namesCount[MyToolBox.TOGGLEBUTTON]) {
                        namesCount[MyToolBox.TOGGLEBUTTON] = number;
                    }
                }
            } catch (StringIndexOutOfBoundsException ex) {
            }
        }
    }
    //###########################################################################

    public void handleUndo() {
        toolbar.toolUndo.setEnabled(true);
        undoItem.setEnabled(true);
        ArrayList<MyComponentContainer> myComponentList = new ArrayList<MyComponentContainer>();
        for (MyComponentContainer c : componentList) {
            myComponentList.add(ComponentClone.clone(c));
        }
        UndoHistoryInformation undoHistoryInfo = new UndoHistoryInformation(myComponentList, panel.getSize(), panel.getBackground());
        if (toolbar.toolRedo.isEnabled()) {
            if (undoHistory.size() >= MainWindow.undoHistoryLimit) {
                undoHistory.remove(0);
                undoHistory.remove(0);
                undoHistory.add(undoHistory.get(undoIndex - 2));
                undoHistory.add(undoHistoryInfo);
            } else {
                undoHistory.add(undoHistory.get(undoIndex));
                undoHistory.add(undoHistoryInfo);
            }
            undoIndex = undoHistory.size() - 1;
            toolbar.toolRedo.setEnabled(false);
            redoItem.setEnabled(false);
        } else {
            if (undoHistory.size() >= MainWindow.undoHistoryLimit) {
                undoHistory.remove(0);
                undoIndex = MainWindow.undoHistoryLimit - 1;
            } else {
                undoIndex++;
            }
            undoHistory.add(undoHistoryInfo);
        }
    }
    //###########################################################################

    public void deleteComponent() {
        ArrayList<MyComponentContainer> deletedComponents = new ArrayList<MyComponentContainer>();
        for (MyComponentContainer c : componentList) {
            if (c.isChosen()) {
                c.hideBorder();
                c.setChosen(false);
                panel.remove(c.getComponent());
                selectedComponentList.remove(c.getName());
                deletedComponents.add(c);
                flagDirty = true;
                toolbar.toolSave.setEnabled(true);
                saveItem.setEnabled(true);
            }
        }
        for (MyComponentContainer cDelete : deletedComponents) {
            componentList.remove(cDelete);
        }
        handleUndo();
        formatToolBar.refreshObjects();
        formatToolBar.disableButtons();
        duplicateItem.setEnabled(false);
        deleteItem.setEnabled(false);
        copyItem.setEnabled(false);
        cutItem.setEnabled(false);
        toolbar.toolCopy.setEnabled(false);
        toolbar.toolCut.setEnabled(false);
        panel.repaint();
    }
    //###########################################################################

    public void selectAllComponents() {
        for (MyComponentContainer component : componentList) {
            if (component.isChosen() == false) {
                component.setChosen(true);
                selectedComponentList.add(component.getName());
                component.showBorder();
            }
        }
        if (selectedComponentList.size() == 1) {
            formatToolBar.setSelectedComponent((String) selectedComponentList.get(0));
            formatToolBar.enableButtons(componentList.get(0));
        } else if (selectedComponentList.size() > 0) {
            formatToolBar.setSelectedComponent(null);
            formatToolBar.enableButtons(null);
        }
        if (selectedComponentList.size() > 0) {
            duplicateItem.setEnabled(true);
            deleteItem.setEnabled(true);
            copyItem.setEnabled(true);
            cutItem.setEnabled(true);
            toolbar.toolCopy.setEnabled(true);
            toolbar.toolCut.setEnabled(true);
        }
    }
    //###########################################################################

    public void addComponent(MyComponentContainer component) {
        component.getComponent().setLocation(x1, y1);
        component.getComponent().setSize(width, height);
        panel.add(component.getComponent());
        componentList.add(component);
        flagDirty = true;
        handleUndo();
        formatToolBar.refreshObjects();
        panel.requestFocusInWindow();
        removeComponentSelection();
        formatToolBar.setSelectedComponent(component.getName());
        formatToolBar.enableButtons(component);
        selectedComponentList.add(component.getName());
        component.setChosen(true);
        component.showBorder();
        toolbar.toolSave.setEnabled(true);
        saveItem.setEnabled(true);
        duplicateItem.setEnabled(true);
        deleteItem.setEnabled(true);
        copyItem.setEnabled(true);
        cutItem.setEnabled(true);
        toolbar.toolCopy.setEnabled(true);
        toolbar.toolCut.setEnabled(true);
        component.resizePanel();
        refreshScrolls();
    }
    //###########################################################################

    private class MenuHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JMenuItem menuItem = (JMenuItem) e.getSource();
            if (menuItem.isEnabled() && e.getModifiers() == 16) {
                String menuItemName = e.getActionCommand();
                if (menuItemName.equals("      Close")) {
                    windowClose();
                } else if (menuItemName.equals("Save (CTRL+S)")) {
                    save();
                } else if (menuItemName.equals("      Save As...")) {
                    String formName = JOptionPane.showInputDialog("Enter Form's Name:", "Copy Of " + getTitle());
                    if (formName != null) {
                        boolean flagNameExists = false;
                        for (String str : MainWindow.myForms) {
                            if (formName.equals(str)) {
                                flagNameExists = true;
                            }
                        }
                        if (flagNameExists) {
                            JOptionPane.showMessageDialog(MyFrame.this, "A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
                        } else if (formName.equals("")) {
                            JOptionPane.showMessageDialog(MyFrame.this, "Form Name Cannot Be Blank!");
                        } else {
                            Save.save(formName, panel, componentList, tableName);
                            MainWindow.refreshFormList(formName);
                            flagDirty = false;
                            toolbar.toolSave.setEnabled(false);
                            menuItem.setEnabled(false);
                            setTitle(formName);
                        }
                    }
                    panel.requestFocusInWindow();
                } else if (menuItemName.equals("Print (CTRL+P)")) {
                    boolean flagPrint = false;
                    PrinterJob printerJob = PrinterJob.getPrinterJob();
                    flagPrint = printerJob.printDialog();
                    if (flagPrint) {
                        MyFileChooser myFileChooser = new MyFileChooser();
                        myFileChooser.printFileChooserSelection = myFileChooser.jFileChooser1.showSaveDialog(null);
                    }
                } else if (menuItemName.equals("Undo (CTRL+Z)")) {
                    toolbar.undo();
                } else if (menuItemName.equals("Redo (CTRL+Y)")) {
                    toolbar.redo();
                } else if (menuItemName.equals("Copy (CTRL+C)")) {
                    toolbar.copy();
                } else if (menuItemName.equals("Cut (CTRL+X)")) {
                    toolbar.cut();
                } else if (menuItemName.equals("Paste (CTRL+V)")) {
                    toolbar.paste();
                } else if (menuItemName.equals("Delete (DELETE)")) {
                    deleteComponent();
                } else if (menuItemName.equals("      Select All (CTRL+A)")) {
                    selectAllComponents();
                } else if (menuItemName.equals("Properties")) {
                    //
                } else if (menuItemName.equals("Formatting")) {
                    formatMode = !formatMode;
                    formatItem.setSelected(formatMode);
                    if (formatMode) {
                        if (formDesignMode) {
                            panelToolBars.setLayout(new GridLayout(2, 1));
                            panelToolBars.add(formatToolBar);
                            validate();
                            repaint();
                        } else {
                            panelToolBars.setLayout(new GridLayout(1, 1));
                            panelToolBars.add(formatToolBar);
                            validate();
                            repaint();
                        }
                    } else {
                        panelToolBars.setLayout(new GridLayout(1, 1));
                        panelToolBars.remove(formatToolBar);
                        validate();
                        repaint();
                    }
                } else if (menuItemName.equals("Form Design")) {
                    formDesignMode = !formDesignMode;
                    formDesignItem.setSelected(formDesignMode);
                    if (formDesignMode) {
                        if (formatMode) {
                            panelToolBars.setLayout(new GridLayout(2, 1));
                            panelToolBars.add(toolbar);
                            validate();
                            repaint();
                        } else {
                            panelToolBars.setLayout(new GridLayout(1, 1));
                            panelToolBars.add(toolbar);
                            validate();
                            repaint();
                        }
                    } else {
                        panelToolBars.setLayout(new GridLayout(1, 1));
                        panelToolBars.remove(toolbar);
                        validate();
                        repaint();
                    }
                } else if (menuItemName.equals("Grid")) {
                    gridMode = !gridMode;
                    gridItem.setSelected(gridMode);
                    panel.repaint();
                } else if (menuItemName.equals("ToolBox")) {
                    toolBoxMode = !toolBoxMode;
                    toolBox2Item.setSelected(toolBoxMode);
                    toolBoxItem.setSelected(toolBoxMode);
                    toolbar.toolToolbox.setSelected(toolBoxMode);

                    if (toolBoxMode) {
                        add(toolbox, BorderLayout.WEST);
                        validate();
                        repaint();
                    } else {
                        remove(toolbox);
                        validate();
                        repaint();
                    }
                    panel.requestFocusInWindow();
                } else if (menuItemName.equals("Form View")) {
                    OutputPanel outputPanel = new OutputPanel(MainWindow.dbCon, componentList, panel.getBackground(), panel.getSize(), MainWindow.recordsLimit);
                    MainPanel mainPanel = new MainPanel(outputPanel.compPanel);
                    outputPanel.compPanel.setLocation(0, 0);
                    mainPanel.drawPanel.add(outputPanel.compPanel);
                    mainPanel.insertRecordNavigationMenu(outputPanel);
                    OutputFrame outputFrame = new OutputFrame(mainPanel);
                    outputFrame.refreshScrolls();
                    outputFrame.setTitle(getTitle());
                    outputFrame.setVisible(true);
                    if (flagDirty) {
                        Save.save(getTitle(), panel, componentList, tableName);
                    }
                    dispose();
                } else if (menuItemName.equals("      Duplicate")) {
                    toolbar.copy();
                    toolbar.paste();
                    MainWindow.copiedComponents.clear();
                    pasteItem.setEnabled(false);
                    toolbar.toolPaste.setEnabled(false);
                } else if (menuItemName.equals("About")) {
                    new About();
                }
            }
        }
    }
    //###########################################################################

    public void save() {
        if (flagNewDesign) {
            String formName = JOptionPane.showInputDialog("Enter Form's Name:", getTitle());
            if (formName != null) {
                boolean flagNameExists = false;
                for (String str : MainWindow.myForms) {
                    if (formName.equals(str)) {
                        flagNameExists = true;
                    }
                }
                if (flagNameExists) {
                    JOptionPane.showMessageDialog(MyFrame.this, "A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
                } else if (formName.equals("")) {
                    JOptionPane.showMessageDialog(MyFrame.this, "Form Name Cannot Be Blank!");
                } else {
                    Save.save(formName, panel, componentList, tableName);
                    MainWindow.refreshFormList(formName);
                    flagDirty = false;
                    toolbar.toolSave.setEnabled(false);
                    saveItem.setEnabled(false);
                }
            }
            panel.requestFocusInWindow();
        } else {
            Save.save(getTitle(), panel, componentList, tableName);
            flagDirty = false;
            toolbar.toolSave.setEnabled(false);
            saveItem.setEnabled(false);
        }
    }
    //###########################################################################

    public class PanelRightClickMenuHandler implements ActionListener {

        public void actionPerformed(ActionEvent a) {
            String name = a.getActionCommand();
            if (name.equals("      Properties")) {
            } else if (name.equals("Paste (CTRL+V)")) {
                toolbar.paste();
            } else if (name.equals("      Select All (CTRL+A)")) {
                selectAllComponents();
            } else if (name.equals("   Grid")) {
                gridMode = !gridMode;
                gridItem.setSelected(gridMode);
                panel.repaint();
            } else if (name.equals("   ToolBox")) {
                toolBoxMode = !toolBoxMode;
                toolBox2Item.setSelected(toolBoxMode);
                toolBoxItem.setSelected(toolBoxMode);
                toolbar.toolToolbox.setSelected(toolBoxMode);

                if (toolBoxMode) {
                    add(toolbox, BorderLayout.WEST);
                    validate();
                    repaint();
                } else {
                    remove(toolbox);
                    validate();
                    repaint();
                }
                panel.requestFocusInWindow();
            } else if (name.equals("Fill/Back Color")) {
                JColorChooser backColorChooser = new JColorChooser();
                Color color = backColorChooser.showDialog(MyFrame.this, "Fill/BackGround Color", null);
                if (color != null) {
                    panel.setBackground(color);
                }
            }
        }
    }
}
